export function fileParse(file, type = "base64") {
    return new Promise(resolve => {
        let fileRead = new FileReader();
        if (type === "base64") {
            fileRead.readAsDataURL(file);
        } else if (type === "buffer") {
            fileRead.readAsArrayBuffer(file);
        }
        fileRead.onload = (ev) => {
            resolve(ev.target.result);
        };
    });
};

/**
* base64转为文件流
* urlString: base64图片格式字符串
* filename：文件名称
*/
export function base64toFile(urlString, fileName) {
 const dataArr = urlString.split(",");
 const byteString = atob(dataArr[1]);
 const options = {
   type: "image/png",
   endings: "native"
 };
 const u8Arr = new Uint8Array(byteString.length);
 for (let i = 0; i < byteString.length; i++) {
   u8Arr[i] = byteString.charCodeAt(i);
 }
 return new File([u8Arr], fileName + ".png", options);//返回文件流
}

/**
 * 文件流转为base64
 * res：文件流
 */
export function fileToBase64(res) {
    return "data:image/png;base64," +
    btoa(
      new Uint8Array(res).reduce(
        (data, byte) => data + String.fromCharCode(byte),
        ""
      )
    )
  }